import pymysql

# no = int(input('学号：'))
# name = input('学校名称：')
# location = input('网站：')

# 第一步：指定主机、端口、用户名、口令、数据库、字符集创建连接
conn = pymysql.connect(host='127.0.0.1', port=3306,
                       user='root', password='root',
                       database='school', charset='utf8',
                       autocommit=True)
# print(conn)
try:
    with conn.cursor() as cursor:
        v_col_id = 105
        v_col_name = "古墓派"
        v_col_addr = "终南山"
        rlt = cursor.execute('insert into collage (collageid,collagename,collageaddr) values(%s,%s,%s)',
                             (v_col_id, v_col_name, v_col_addr))
        conn.commit()
        print(rlt)
except pymysql.MySQLError as err:
    print(err)
finally:
    conn.close()
# try:
#     # 第二步：通过连接对象的cursor方法获取游标对象
#     with conn.cursor() as cursor:
#         # 第三步：通过游标对象的execute方法向数据库发出SQL
#         result = cursor.execute(
#             'insert into tb_dept values (%s, %s, %s)',
#             (no, name, location)
#         )
#         if result == 1:
#             print('新增部门成功!!!')
#     # 第四步：所有操作都成功就提交
#     # conn.commit()
# except pymysql.MySQLError as err:
#     print(err)
#     # 第四步：如果出现异常就回滚（撤销）
#     # conn.rollback()
# finally:
#     # 第五步：关闭连接释放资源
#     conn.close()
